5 * @extends OO.ui.Widget
8 * @param {mw.rcfilters.dm.FiltersViewModel} filtersViewModel View model
9 * @param {mw.rcfilters.dm.ChangesListViewModel} changesListViewModel View model
10 * @param {mw.rcfilters.Controller} controller
11 * @param {jQuery} $changesListRoot Root element of the changes list to attach to
12 * @param {Object} [config] Configuration object
14 mw
.rcfilters
.ui
.ChangesListWrapperWidget
= function MwRcfiltersUiChangesListWrapperWidget(
21 config
= $.extend( {}, config
, {
22 $element
: $changesListRoot
26 mw
.rcfilters
.ui
.ChangesListWrapperWidget
.parent
.call( this, config
);
28 this.filtersViewModel
= filtersViewModel
;
29 this.changesListViewModel
= changesListViewModel
;
30 this.controller
= controller
;
31 this.highlightClasses
= null;
32 this.filtersModelInitialized
= false;
35 this.filtersViewModel
.connect( this, {
36 itemUpdate
: 'onItemUpdate',
37 highlightChange
: 'onHighlightChange',
38 initialize
: 'onFiltersModelInitialize'
40 this.changesListViewModel
.connect( this, {
41 invalidate
: 'onModelInvalidate',
42 update
: 'onModelUpdate',
43 newChangesExist
: 'onNewChangesExist'
47 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget' )
48 // We handle our own display/hide of the empty results message
49 // We keep the timeout class here and remove it later, since at this
50 // stage it is still needed to identify that the timeout occurred.
51 .removeClass( 'mw-changeslist-empty' );
53 this.setupNewChangesButtonContainer();
58 OO
.inheritClass( mw
.rcfilters
.ui
.ChangesListWrapperWidget
, OO
.ui
.Widget
);
61 * Respond to filters model initialize event
63 mw
.rcfilters
.ui
.ChangesListWrapperWidget
.prototype.onFiltersModelInitialize = function () {
64 this.filtersModelInitialized
= true;
65 // Set up highlight containers. We need to wait for the filters model
66 // to be initialized, so we can make sure we have all the css class definitions
67 // we get from the server with our filters
68 this.setupHighlightContainers( this.$element
);
72 * Get all available highlight classes
74 * @return {string[]} An array of available highlight class names
76 mw
.rcfilters
.ui
.ChangesListWrapperWidget
.prototype.getHighlightClasses = function () {
77 if ( !this.highlightClasses
|| !this.highlightClasses
.length
) {
78 this.highlightClasses
= this.filtersViewModel
.getItemsSupportingHighlights()
79 .map( function ( filterItem
) {
80 return filterItem
.getCssClass();
84 return this.highlightClasses
;
88 * Respond to the highlight feature being toggled on and off
90 * @param {boolean} highlightEnabled
92 mw
.rcfilters
.ui
.ChangesListWrapperWidget
.prototype.onHighlightChange = function ( highlightEnabled
) {
93 if ( highlightEnabled
) {
94 this.applyHighlight();
96 this.clearHighlight();
101 * Respond to a filter item model update
103 mw
.rcfilters
.ui
.ChangesListWrapperWidget
.prototype.onItemUpdate = function () {
104 if ( this.filtersModelInitialized
&& this.filtersViewModel
.isHighlightEnabled() ) {
105 this.clearHighlight();
106 this.applyHighlight();
111 * Respond to changes list model invalidate
113 mw
.rcfilters
.ui
.ChangesListWrapperWidget
.prototype.onModelInvalidate = function () {
114 $( 'body' ).addClass( 'mw-rcfilters-ui-loading' );
118 * Respond to changes list model update
120 * @param {jQuery|string} $changesListContent The content of the updated changes list
121 * @param {jQuery} $fieldset The content of the updated fieldset
122 * @param {string} noResultsDetails Type of no result error
123 * @param {boolean} isInitialDOM Whether $changesListContent is the existing (already attached) DOM
124 * @param {boolean} from Timestamp of the new changes
126 mw
.rcfilters
.ui
.ChangesListWrapperWidget
.prototype.onModelUpdate = function (
127 $changesListContent
, $fieldset
, noResultsDetails
, isInitialDOM
, from
129 var conflictItem
, noResultsKey
,
130 $message
= $( '<div>' )
131 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-results' ),
132 isEmpty
= $changesListContent
=== 'NO_RESULTS',
133 // For enhanced mode, we have to load these modules, which are
134 // not loaded for the 'regular' mode in the backend
135 loaderPromise
= mw
.user
.options
.get( 'usenewrc' ) ?
136 mw
.loader
.using( [ 'mediawiki.special.changeslist.enhanced', 'mediawiki.icon' ] ) :
137 $.Deferred().resolve(),
140 this.$element
.toggleClass( 'mw-changeslist', !isEmpty
);
142 this.$element
.empty();
144 if ( this.filtersViewModel
.hasConflict() ) {
145 conflictItem
= this.filtersViewModel
.getFirstConflictedItem();
150 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-results-conflict' )
151 .text( mw
.message( 'rcfilters-noresults-conflict' ).text() ),
153 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-results-message' )
154 .text( mw
.message( conflictItem
.getCurrentConflictResultMessage() ).text() )
158 ( noResultsDetails
=== 'NO_RESULTS_TIMEOUT' ) ?
159 'recentchanges-timeout' :
160 ( noResultsDetails
=== 'NO_RESULTS_NETWORK_ERROR' ) ?
161 'recentchanges-network' :
162 'recentchanges-noresult';
167 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-results-noresult' )
168 .text( mw
.message( noResultsKey
).text() )
171 this.$element
.removeClass( 'mw-changeslist-timeout' );
174 this.$element
.append( $message
);
176 if ( !isInitialDOM
) {
177 this.$element
.empty().append( $changesListContent
);
180 this.emphasizeNewChanges( from );
184 // Set up highlight containers
185 this.setupHighlightContainers( this.$element
);
188 this.applyHighlight();
192 loaderPromise
.done( function () {
193 if ( !isInitialDOM
&& !isEmpty
) {
194 // Make sure enhanced RC re-initializes correctly
195 mw
.hook( 'wikipage.content' ).fire( widget
.$element
);
198 $( 'body' ).removeClass( 'mw-rcfilters-ui-loading' );
203 * Emphasize the elements (or groups) newer than the 'from' parameter
204 * @param {string} from Anything newer than this is considered 'new'
206 mw
.rcfilters
.ui
.ChangesListWrapperWidget
.prototype.emphasizeNewChanges = function ( from ) {
209 $newChanges
= $( [] ),
210 selector
= this.inEnhancedMode() ?
211 'table.mw-enhanced-rc[data-mw-ts]' :
213 set = this.$element
.find( selector
),
216 set.each( function ( index
) {
217 var $this = $( this ),
218 ts
= $this.data( 'mw-ts' );
221 $newChanges
= $newChanges
.add( $this );
224 // guards against putting the marker after the last element
225 if ( index
=== ( length
- 1 ) ) {
232 $indicator
= $( '<div>' )
233 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-previousChangesIndicator' );
235 $firstNew
.after( $indicator
);
244 * Respond to changes list model newChangesExist
246 * @param {boolean} newChangesExist Whether new changes exist
248 mw
.rcfilters
.ui
.ChangesListWrapperWidget
.prototype.onNewChangesExist = function ( newChangesExist
) {
249 this.showNewChangesLink
.toggle( newChangesExist
);
253 * Respond to the user clicking the 'show new changes' button
255 mw
.rcfilters
.ui
.ChangesListWrapperWidget
.prototype.onShowNewChangesClick = function () {
256 this.controller
.showNewChanges();
260 * Setup the container for the 'new changes' button.
262 mw
.rcfilters
.ui
.ChangesListWrapperWidget
.prototype.setupNewChangesButtonContainer = function () {
263 this.showNewChangesLink
= new OO
.ui
.ButtonWidget( {
265 label
: mw
.message( 'rcfilters-show-new-changes' ).text(),
266 flags
: [ 'progressive' ]
268 this.showNewChangesLink
.connect( this, { click
: 'onShowNewChangesClick' } );
269 this.showNewChangesLink
.toggle( false );
271 // HACK: Add the -newChanges div inside rcfilters-head, rather than right above us
272 // Visually it's the same place, but by putting it inside rcfilters-head we are
273 // able to use the min-height rule to prevent the page from jumping when this is added.
274 this.$element
.parent().find( '.rcfilters-head' ).append(
276 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-newChanges' )
277 .append( this.showNewChangesLink
.$element
)
282 * Set up the highlight containers with all color circle indicators.
284 * @param {jQuery|string} $content The content of the updated changes list
286 mw
.rcfilters
.ui
.ChangesListWrapperWidget
.prototype.setupHighlightContainers = function ( $content
) {
287 var $enhancedTopPageCell
, $enhancedNestedPagesCell
,
289 highlightClass
= 'mw-rcfilters-ui-changesListWrapperWidget-highlights',
290 $highlights
= $( '<div>' )
291 .addClass( highlightClass
)
294 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlights-circle' )
295 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlights-color-none' )
296 .prop( 'data-color', 'none' )
299 if ( $( '.mw-rcfilters-ui-changesListWrapperWidget-highlights' ).length
) {
304 mw
.rcfilters
.HighlightColors
.forEach( function ( color
) {
307 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlights-color-' + color
)
308 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlights-circle' )
309 .prop( 'data-color', color
)
313 if ( this.inEnhancedMode() ) {
314 $enhancedTopPageCell
= $content
.find( 'table.mw-enhanced-rc.mw-collapsible' );
315 $enhancedNestedPagesCell
= $content
.find( 'td.mw-enhanced-rc-nested' );
317 // Enhanced RC highlight containers
318 $content
.find( 'table.mw-enhanced-rc tr:first-child' )
319 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-enhanced-toplevel' )
322 .append( $highlights
.clone() )
325 // We are adding and changing cells in a table that, despite having nested rows,
326 // is actually all one big table. To prevent the highlights cell in the "nested"
327 // rows from stretching out the cell with the flags and timestamp in the top row,
328 // we give the latter colspan=2. Then to make things line up again, we add
329 // an empty <td> to the "nested" rows.
331 // Set colspan=2 on cell with flags and timestamp in top row
332 $content
.find( 'table.mw-enhanced-rc tr:first-child td.mw-enhanced-rc' )
333 .prop( 'colspan', '2' );
334 // Add empty <td> to nested rows to compensate
335 $enhancedNestedPagesCell
.parent().prepend( $( '<td>' ) );
336 // Add highlights cell to nested rows
337 $enhancedNestedPagesCell
340 .append( $highlights
.clone().addClass( 'mw-enhanced-rc-nested' ) )
343 // We need to target the nested rows differently than the top rows so that the
344 // LESS rules applies correctly. In top rows, the rule should highlight all but
345 // the first 2 cells td:not( :nth-child( -n+2 ) and the nested rows, the rule
346 // should highlight all but the first 4 cells td:not( :nth-child( -n+4 )
347 $enhancedNestedPagesCell
349 .addClass( 'mw-rcfilters-ui-changesListWrapperWidget-enhanced-nested' );
351 // Go over pages that have sub results
352 // HACK: We really only can collect those by targetting the collapsible class
353 $enhancedTopPageCell
.each( function () {
354 var collectedClasses
,
357 // Go over <tr>s and pick up all recognized classes
358 collectedClasses
= widget
.getHighlightClasses().filter( function ( className
) {
359 return $table
.find( 'tr' ).hasClass( className
);
362 $table
.find( 'tr:first-child' )
363 .addClass( collectedClasses
.join( ' ' ) );
366 $content
.addClass( 'mw-rcfilters-ui-changesListWrapperWidget-enhancedView' );
369 $content
.find( 'ul.special li' )
370 .prepend( $highlights
.clone() );
375 * In enhanced mode, we need to check whether the grouped results all have the
376 * same active highlights in order to see whether the "parent" of the group should
377 * be grey or highlighted normally.
379 * This is called every time highlights are applied.
381 mw
.rcfilters
.ui
.ChangesListWrapperWidget
.prototype.updateEnhancedParentHighlight = function () {
382 var activeHighlightClasses
,
383 $enhancedTopPageCell
= this.$element
.find( 'table.mw-enhanced-rc.mw-collapsible' );
385 activeHighlightClasses
= this.filtersViewModel
.getCurrentlyUsedHighlightColors().map( function ( color
) {
386 return 'mw-rcfilters-highlight-color-' + color
;
389 // Go over top pages and their children, and figure out if all sub-pages have the
390 // same highlights between themselves. If they do, the parent should be highlighted
391 // with all colors. If classes are different, the parent should receive a grey
393 $enhancedTopPageCell
.each( function () {
394 var firstChildClasses
, $rowsWithDifferentHighlights
,
397 // Collect the relevant classes from the first nested child
398 firstChildClasses
= activeHighlightClasses
.filter( function ( className
) {
399 return $table
.find( 'tr:nth-child(2)' ).hasClass( className
);
401 // Filter the non-head rows and see if they all have the same classes
403 $rowsWithDifferentHighlights
= $table
.find( 'tr:not(:first-child)' ).filter( function () {
404 var classesInThisRow
,
407 classesInThisRow
= activeHighlightClasses
.filter( function ( className
) {
408 return $this.hasClass( className
);
411 return !OO
.compare( firstChildClasses
, classesInThisRow
);
414 // If classes are different, tag the row for using grey color
415 $table
.find( 'tr:first-child' )
416 .toggleClass( 'mw-rcfilters-ui-changesListWrapperWidget-enhanced-grey', $rowsWithDifferentHighlights
.length
> 0 );
421 * @return {boolean} Whether the changes are grouped by page
423 mw
.rcfilters
.ui
.ChangesListWrapperWidget
.prototype.inEnhancedMode = function () {
424 var uri
= new mw
.Uri();
425 return ( uri
.query
.enhanced
!== undefined && Number( uri
.query
.enhanced
) ) ||
426 ( uri
.query
.enhanced
=== undefined && Number( mw
.user
.options
.get( 'usenewrc' ) ) );
430 * Apply color classes based on filters highlight configuration
432 mw
.rcfilters
.ui
.ChangesListWrapperWidget
.prototype.applyHighlight = function () {
433 if ( !this.filtersViewModel
.isHighlightEnabled() ) {
437 this.filtersViewModel
.getHighlightedItems().forEach( function ( filterItem
) {
438 var $elements
= this.$element
.find( '.' + filterItem
.getCssClass() );
440 // Add highlight class to all highlighted list items
443 'mw-rcfilters-highlighted ' +
444 'mw-rcfilters-highlight-color-' + filterItem
.getHighlightColor()
447 // Track the filters for each item in .data( 'highlightedFilters' )
448 $elements
.each( function () {
449 var filters
= $( this ).data( 'highlightedFilters' );
452 $( this ).data( 'highlightedFilters', filters
);
454 if ( filters
.indexOf( filterItem
.getLabel() ) === -1 ) {
455 filters
.push( filterItem
.getLabel() );
459 // Apply a title to each highlighted item, with a list of filters
460 this.$element
.find( '.mw-rcfilters-highlighted' ).each( function () {
461 var filters
= $( this ).data( 'highlightedFilters' );
463 if ( filters
&& filters
.length
) {
464 $( this ).attr( 'title', mw
.msg(
465 'rcfilters-highlighted-filters-list',
466 filters
.join( mw
.msg( 'comma-separator' ) )
471 if ( this.inEnhancedMode() ) {
472 this.updateEnhancedParentHighlight();
475 // Turn on highlights
476 this.$element
.addClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlighted' );
480 * Remove all color classes
482 mw
.rcfilters
.ui
.ChangesListWrapperWidget
.prototype.clearHighlight = function () {
483 // Remove highlight classes
484 mw
.rcfilters
.HighlightColors
.forEach( function ( color
) {
486 .find( '.mw-rcfilters-highlight-color-' + color
)
487 .removeClass( 'mw-rcfilters-highlight-color-' + color
);
490 this.$element
.find( '.mw-rcfilters-highlighted' )
491 .removeAttr( 'title' )
492 .removeData( 'highlightedFilters' )
493 .removeClass( 'mw-rcfilters-highlighted' );
495 // Remove grey from enhanced rows
496 this.$element
.find( '.mw-rcfilters-ui-changesListWrapperWidget-enhanced-grey' )
497 .removeClass( 'mw-rcfilters-ui-changesListWrapperWidget-enhanced-grey' );
499 // Turn off highlights
500 this.$element
.removeClass( 'mw-rcfilters-ui-changesListWrapperWidget-highlighted' );